import {PlantValueMap} from "../../common/config.js";


export default class PlantAbstract extends Phaser.Physics.Matter.Sprite {
    constructor(scene, x, y, texture, frame,plantValue) {
        super(scene.matter.world, x, y, texture, frame);
        this.destroyed = false;
        // 确保抽象类不能被直接实例化
        if (new.target === PlantAbstract) {
            throw new Error("Cannot instantiate abstract class AbstractSprite.");
        }
        this.scene = scene;
        this.plantValue = plantValue

        this.hp= this.plantValue.HP
        console.log("Binding updateTime");
        this.scene.events.on("update", this.updateTime, this);
        // 初始化
    }
    // 受到攻击的方法
    takeDamage(amount=1) {
        // 减少生命值
        console.log("减少生命值",this.hp)
        console.log("减少生命值",amount)
        this.hp -= amount;
        if (this.hp <= 1) {
            console.log("死亡")
            this.destroy();
        }
        return  this.hp
    }
    update(time){
        throw new Error("Abstract method update must be implemented in derived class.");
    }
    updateTime() {
        console.log("updateTime-------------------------------")
        if( this.destroyed)return
        this.update(this.scene.curTime)
    }
    destroy() {
        this.destroyed = true; // 标记为已销毁
        console.log("Unbinding updateTime");
        this.scene.events.off("update", this.updateTime, this);
        console.log("destroy----------this.scene.events.off(\"update\", this.updateTime, this);---------------------")
        super.destroy();
    }

}